package com.stone.elec.rename.executor.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import com.stone.elec.rename.config.ReNameConfig;
import com.stone.elec.rename.executor.BaseExecutor;
import com.stone.elec.rename.executor.ExecutorFactory;
import com.stone.elec.rename.utils.FileUtils;

public class ProjectDirExecutor implements BaseExecutor {

    private String path = "";

    private ReNameConfig reNameConfig;

    private static ProjectDirExecutor projectExecutor = new ProjectDirExecutor();

    public ProjectDirExecutor() {
        reNameConfig = ReNameConfig.reNameConfig;
    }

    public static void put() {
        ExecutorFactory.put("projectDir", projectExecutor);
    }

    @Override
    public void execute() {
        //修改db
        File[] fileName = FileUtil.ls(reNameConfig.getBasePath() + "\\" + path);
        List<File> project = new ArrayList<>();
        for (File file : fileName) {
            if (FileUtil.isDirectory(file)) {
                if (!FileUtils.fileDicNext(file)) {
                    continue;
                }
                if (file.getName().equals("proj-rename")) {
                    continue;
                }
                project.add(file);
            }else{
                FileUtils.projectFileParse(file);
            }
        }

        if (CollUtil.isNotEmpty(project)) {

            project.stream().forEach(v -> {
                projectParse(v);
            });
        }
    }

    private void projectParse(File file) {
        if (FileUtil.isDirectory(file)) {
            //获得第一层结构
            projectDirParse(file);
        } else {
            FileUtils.projectFileParse(file);
        }
    }



    private void projectDirParse(File file) {

        //获取子文件夹
        File[] fileName = FileUtils.ls(file);

        if (ArrayUtil.isNotEmpty(fileName)) {
            Arrays.stream(fileName).forEach(v -> {
                projectParse(v);
            });
        }
        //先验证文件名
        FileUtils.fileDicValidate(file);

    }
}
